﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ChuanYe.CountyFireManagement.Api.Models;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ChuanYe.CountyFireManagement.Api.Controllers
{
    /// <summary>
    /// 火灾扑救力量
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FireFightingForceController : ControllerBase
    {
        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase Add(FireFightingForceRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                string ip = HttpContext.GetClientIp();
                List<Firefightingforce> infos = new List<Firefightingforce>();
                foreach (var item in obj.FightingTeamNums)
                {
                    Firefightingforce info = new Firefightingforce();
                    info.FireId = obj.FireId;
                    info.TransDatetime = obj.TransDatetime.HasValue ? obj.TransDatetime.Value : DateTime.Now;
                    info.UserId = obj.UserId;
                    info.Ip = ip;
                    info.HeadquarterId = item.HeadquarterId;
                    info.TeamNum = item.TeamNum;
                    infos.Add(info);
                }
                var data = BLL.FireFightingForceBLL.Instance.Add(infos);
                if (data.FirstParam != null)
                {
                    ControllerHelper.SendMessage(data.FirstParam);
                    var allExpireFreInfo = BLL.FireExpireBLL.Instance.GetUnExpire();
                    MqttHub.fireexpires = allExpireFreInfo;
                }
                result.Status=data.Status;
                result.Message = data.Message;
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/FireFightingForce/Add Server Error";
                LogHelper.Error(result.Message,ex);
            }
            return result;
        }

        /// <summary>
        ///  扑救力量 下属单位回复 
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase Reply(FireForceSupportReplyReqeust obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                Fireforcesupport info = new Fireforcesupport();
                info.FireId = obj.FireID;
                info.ForceSupportContent = string.Empty;
                info.TransDatetime = obj.TransDatetime.HasValue ? obj.TransDatetime.Value : DateTime.Now;
                info.UserId = obj.UserId;
                List<SubFireForceSupportModel> subInfos =new List<SubFireForceSupportModel>();
                foreach (var item in obj.SubFireForceSupports)
                {
                    SubFireForceSupportModel subInfo = new SubFireForceSupportModel();
                    subInfo.CaptainName = item.CaptainName;
                    subInfo.CaptainPhone = item.CaptainPhone;
                    subInfo.RankName = item.RankName;
                    subInfo.Sum = item.Sum;
                    subInfos.Add(subInfo);
                }

                var data= BLL.FireFightingForceBLL.Instance.Reply(info,subInfos);
                if (data.FirstParam != null)
                {
                    ControllerHelper.SendMessage(data.FirstParam);
                    var allExpireFreInfo = BLL.FireExpireBLL.Instance.GetUnExpire();
                    MqttHub.fireexpires = allExpireFreInfo;
                }
                result.Status = data.Status;
                result.Message = data.Message;
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/FireFightingForce/Reply Error";
                LogHelper.Error(result.Message,ex);
            }
            return result;
        }

    }
}